package com.qaml.mes.device.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.earth.user.AuthSsoContext;
import com.qaml.mes.device.dao.CheckUpkeepInfoDO;
import com.qaml.mes.device.dao.mapper.CheckUpkeepInfoMapper;
import com.qaml.mes.device.dto.eqp.CheckUpkeepInfoListDTO;
import com.qaml.mes.device.enums.DeletedEnum;
import com.qaml.mes.device.repository.CheckUpkeepInfoRepository;
import com.qaml.mes.device.dto.eqp.CheckUpkeepInfoDTO;
import com.qaml.mes.device.enums.UseStatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.lang.invoke.LambdaMetafactory;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName CheckUpkeepInfoRepositoryImpl
 * @Description TODO
 * @Author leikai
 * @Date 2024/2/27 0027 9:48
 * @Version 1.0
 */
@Repository
public class CheckUpkeepInfoRepositoryImpl implements CheckUpkeepInfoRepository {

    @Resource
    private CheckUpkeepInfoMapper checkUpkeepInfoMapper;

    @Override
    public void update(CheckUpkeepInfoDO checkUpkeepInfoDO) {
        checkUpkeepInfoMapper.updateById(checkUpkeepInfoDO);
    }

    @Override
    public void save(CheckUpkeepInfoDO checkUpkeepInfoDO) {
        checkUpkeepInfoMapper.insert(checkUpkeepInfoDO);
    }

    /**
     * 根据检保项目编号查询检保信息
     *
     * @param checkUpkeepId
     * @return
     */
    @Override
    public CheckUpkeepInfoDO getBycheckUpkeepId(String checkUpkeepId) {
        return checkUpkeepInfoMapper.selectBycheckUpkeepId(checkUpkeepId);
    }


    @Override
    public CheckUpkeepInfoDO getById(String checkUpkeepId) {
        return checkUpkeepInfoMapper.selectById(checkUpkeepId);
    }

    /**
     * 分页查询检保信息
     *
     * @param page
     * @param pageSize
     * @param checkUpkeepInfoDTO
     * @return
     */
    @Override
    public IPage<CheckUpkeepInfoDO> page(Integer page, Integer pageSize, CheckUpkeepInfoListDTO checkUpkeepInfoListDTO) {

        return checkUpkeepInfoMapper.selectPage(new Page<>(page, pageSize), buildQueryWrapper(checkUpkeepInfoListDTO));
    }


    /**
     * 根据设备id查询检保信息
     *
     * @param eqpId
     * @return
     */
    @Override
    public List<CheckUpkeepInfoDO> getByEqpId(String eqpId) {

        LambdaQueryWrapper<CheckUpkeepInfoDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckUpkeepInfoDO::getEqpId, eqpId);
        queryWrapper.eq(CheckUpkeepInfoDO::getStatus, 1);
        queryWrapper.eq(CheckUpkeepInfoDO::getIsDeleted, 0);

        return checkUpkeepInfoMapper.selectList(queryWrapper);

    }

    /**
     * 根据设备id更新检保信息
     *
     * @param checkUpkeepInfoDOS
     */
    @Override
    public void updateByEqpId(List<CheckUpkeepInfoDO> checkUpkeepInfoDOS) {
        for (CheckUpkeepInfoDO checkUpkeepInfoDO : checkUpkeepInfoDOS) {
            checkUpkeepInfoMapper.updateById(checkUpkeepInfoDO);
        }
    }

    @Override
    public void deleteById(String id) {
        checkUpkeepInfoMapper.deleteOne(id);
    }

    /**
     * 根据设备id和状态查询检保信息
     *
     * @param eqpId
     * @param type
     * @return
     */
    @Override
    public List<CheckUpkeepInfoDO> getByEqpIdAndStatus(String eqpId, Integer type) {
        LambdaQueryWrapper<CheckUpkeepInfoDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CheckUpkeepInfoDO::getEqpId, eqpId);
        lambdaQueryWrapper.eq(type != null, CheckUpkeepInfoDO::getType, type);
        lambdaQueryWrapper.eq(CheckUpkeepInfoDO::getIsDeleted, 0);
        return checkUpkeepInfoMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public List<CheckUpkeepInfoDO> getByEqpIdAndParams(String eqpId, CheckUpkeepInfoDO params) {
        LambdaQueryWrapper<CheckUpkeepInfoDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CheckUpkeepInfoDO::getEqpId, eqpId);
        if (Objects.nonNull(params)) {
            lambdaQueryWrapper.eq(Objects.nonNull(params.getType()), CheckUpkeepInfoDO::getType, params.getType());
            lambdaQueryWrapper.eq(Objects.nonNull(params.getStatus()), CheckUpkeepInfoDO::getStatus, params.getStatus());
        }

        lambdaQueryWrapper.eq(CheckUpkeepInfoDO::getIsDeleted, 0);
        return checkUpkeepInfoMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 根据id和状态查询检保信息
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public CheckUpkeepInfoDO getByIdAndStatus(String id, Integer status) {
        LambdaQueryWrapper<CheckUpkeepInfoDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CheckUpkeepInfoDO::getId, id);
        lambdaQueryWrapper.eq(CheckUpkeepInfoDO::getStatus, status);
        lambdaQueryWrapper.eq(CheckUpkeepInfoDO::getIsDeleted, 0);
        return checkUpkeepInfoMapper.selectOne(lambdaQueryWrapper);
    }


    @Override
    public List<CheckUpkeepInfoDO> getCheckUpkeepInfoDOByType(String type) {
        LambdaQueryWrapper<CheckUpkeepInfoDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CheckUpkeepInfoDO::getType, type);
        lambdaQueryWrapper.eq(CheckUpkeepInfoDO::getIsDeleted, DeletedEnum.NO.getCode());
        lambdaQueryWrapper.eq(CheckUpkeepInfoDO::getStatus, UseStatusEnum.ENABLED.getType());
        return checkUpkeepInfoMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public List<CheckUpkeepInfoDO> getCheckUpkeepInfoDOByTypeAndCycle(String type, String cycle) {
        LambdaQueryWrapper<CheckUpkeepInfoDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CheckUpkeepInfoDO::getType, type);
        lambdaQueryWrapper.eq(CheckUpkeepInfoDO::getIsDeleted, DeletedEnum.NO.getCode());
        lambdaQueryWrapper.eq(CheckUpkeepInfoDO::getCycle, cycle);
        lambdaQueryWrapper.eq(CheckUpkeepInfoDO::getStatus, UseStatusEnum.ENABLED.getType());
        return checkUpkeepInfoMapper.selectList(lambdaQueryWrapper);
    }

    private LambdaQueryWrapper<CheckUpkeepInfoDO> buildQueryWrapper(CheckUpkeepInfoListDTO checkUpkeepInfoListDTO) {
        LambdaQueryWrapper<CheckUpkeepInfoDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckUpkeepInfoDO::getIsDeleted, 0);
        queryWrapper.eq(CheckUpkeepInfoDO::getTenantId, AuthSsoContext.tenantId());
        /**
         * 查询条件 设备名称模糊查询
         * 设备编号查询
         * 检保类型查询
         */
        queryWrapper.like(StringUtils.isNotBlank(checkUpkeepInfoListDTO.getEqpName()), CheckUpkeepInfoDO::getEqpName, checkUpkeepInfoListDTO.getEqpName())
                .eq(StringUtils.isNotBlank(checkUpkeepInfoListDTO.getEqpCode()), CheckUpkeepInfoDO::getEqpCode, checkUpkeepInfoListDTO.getEqpCode())
                .eq(Objects.nonNull(checkUpkeepInfoListDTO.getType()), CheckUpkeepInfoDO::getType, checkUpkeepInfoListDTO.getType())
                .orderByDesc(CheckUpkeepInfoDO::getId);
        return queryWrapper;
    }


}